<?php

/**
 * Mediad co,ltd
 *
 * @category   Mediad2
 * @package    Mediad2_Service
 * @subpackage Util
 * @copyright  Copyright (c) 2012 Mediad co,ltd. (http://www.Mediad.cn)
 * @license    New BSD License
 * @version    $Id: Email.php 1 2012-04-24 14:38:00Z vincent@Mediad.cn 重新优化EMAIL类
 */

require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';

/**
 * @category   Mediad2
 * @package    Service
 * @subpackage Util
 */
class Mediad2_Service_Util_Email extends Zend_Mail{
		
	/**
	 * @var 用户名
	 */
	private $user;
						
	/**
	 * @var 密码
	 */
	private $pwd;
	
	/**
	 * @var SMTP
	 */
	private $smtp;
							
	/**
	 * @var 服务邮箱
	 */
	private $mailBox;			
		
	/**
	 * @var 标题
	 */
	private $subject;

	/**
	 * @var 收件人[]
	 */
	private $addTo;
	
	/**
	 * @var 内容
	 */
	private $body;
	
	public function __construct(Array $mailConfig = null)
    {
    	$config = null;
    	if($mailConfig == null) {
    		$registry = Zend_Registry::getInstance ();
    		if ($registry->isRegistered('mailConfig')) {
    			$config = $registry->get("mailConfig");
    		}
    		if (!isset($config) || empty($config)) {
    			$config = $this->setMailConfig();
    		}
    	}
    	
        if (isset($config['user'])) {
            $this->user = $config['user'];
        }
        if (isset($config['pwd'])) {
            $this->pwd = $config['pwd'];
        }
        if (isset($config['smtp'])) {
            $this->smtp = $config['smtp'];
        }
        if (isset($config['mailbox'])) {
        	$this->mailBox = $config['mailbox'];
        }       
    }
    
    /**
     * 默认邮箱配置
     * @return array email config
     */
    private function setMailConfig()
    {
    	//设置默认值
    	$config = array();
    	$config['user'] = 'webmaster@Mediad.cn';
    	$config['pwd'] = 'Mediad1529';
    	$config['smtp'] = 'smtp.exmail.qq.com';
    	$config['mailbox'] = 'webmaster@Mediad.cn';
    	return $config;
    }
    
    /**
     * 发送邮件
     * @return boolean
     */
    private function sendMail()
    {
    	//验证用户邮箱地址 
    	$arrValidEmail = array();
    	$validator = new Zend_Validate_EmailAddress();
    	if (!is_array($this->addTo)) {
	    	if(!$validator->isValid($this->addTo)) {
	    		return false;
	    	}
	    	$arrValidEmail[] = $this->addTo;
    	} else {
    		foreach ($this->addTo as $key => $email) {
    			if(!$validator->isValid($email)) {
    				return false;
    			}
    			$arrValidEmail[] = $email;
    		}
    	}
    	
    	//将有效的email复制到$this->addTo
    	$this->addTo = $arrValidEmail;
    	
    	//设置信息
    	$config = array(
    			'auth' => 'login',
    			'username' => $this->user,
    			'password' => $this->pwd,
    	);
    	$mail = new Zend_Mail('utf-8');
    	$transport = new Zend_Mail_Transport_Smtp($this->smtp,$config);
    	
    	$mail->setSubject($this->subject);
    	$mail->setFrom($this->mailBox,"Mediad");
    	$mail->addTo($this->addTo,"");
    	$mail->setBodyHtml($this->body);
    	
    	try {
    		$mail->send($transport);
    		return true;
    	}catch (Exception $e){ 
    		$this->sendException($e->getMessage());   		
    		return false;
    	}
    }
    
    
    /**
     * 普通邮件
     * 
     * @param string[] $addTo
     * 收件人
     * 
     * @param string $title
     * 标题
     * 
     * @param html $detail
     * 正文
     * 
     * @return boolean
     * 发送是否成功
     */
    public function sendEmail($addTo, $title = null, $detail)
    {
    	if($title!=null){
    		$this->subject = $title;
    	}else {
    		$this->subject = "Mediad";
    	}    	
    	$this->addTo = $addTo;
    	$this->body = $detail;    	
    	return $this->sendMail();
    }
    
	/**
	 * 发送邮件--用户注册验证
	 * 
	 * @param string[] $addTo 收件人
	 * @param string $key 验证信息
	 * @return boolean
	 */
	public function sendValidEmail($addTo,$key) 
	{	
		//TODO:需要构造用户注册邮件。。。 vincent 0424
		
		$this->subject = "Mediad验证信息";
		$this->addTo = $addTo;
		$date=date('Y.m.d H:i:s');
		$website = WEB_PATH;
		$html="<h3>尊敬的用户<h3/><br/>
		<p>您好！恭喜您成功注册Mediad帐号！</p><br/>
		<p>激活链接如下：</p><br/>
		<p><a href='$website$key'>验证邮箱</a></p><br/><br/><br/>
		<p>Mediad</p><br/>$date";
		
		$this->body = $html;
		return $this->sendMail();		
	}
	
	/**
	 * 发送邮件--系统异常
	 * 
	 * @param string[] $addTo 收件人
	 * @param string $message 异常信息
	 * @return boolean
	 */
	public function sendException($message,$addTo = null) 
	{
		$this->subject = "Mediad--异常:" . substr($message,0,20) . '...';		
		if(empty($addTo)){
			$this->addTo = $this->mailBox;
		}else{
			$this->addTo = $addTo;
		}
		
		$date=date('Y.m.d H:i:s');
		$html="异常信息：<br/>$message<br/><p>Mediad</p><br/>$date";
		$this->body = $html;
		return $this->sendMail();	
	}
	
	/**
	 * 发送重置密码邮件
	 * @param string|array $addTo
	 * @param html $message
	 * @return boolean
	 */
	public function sendResetPasswordEmail($addTo,$token)
	{
		$this->subject = "Mediad";
		$this->addTo = $addTo;
		$date=date('Y.m.d H:i:s');
		$html="<h3>尊敬的用户<h3/><br/>
		<p>您好！您的账户密码重设地址如下:</p><br/>
		<p><a href=\"$token\">地址</a></p><br/>
		<p>请尽快登录并更改密码.</p><br/>
		<br/><br/>
		<p>该重置链接将于72小时后失效。 </p><br/>
		<p>Mediad</p><br/> $date";
		$this->body = $html;
		return $this->sendMail();
	}

}